import sys,os
from subprocess import call

matlock = "/scratch/project/qaafi-cnafs/jingping/software/miniconda3/bin/matlock"
samtools = "/scratch/project/qaafi-cnafs/jingping/software/miniconda3/bin/samtools"
assvis = "/scratch/project_mnt/S0030/jingping/software/3d-dna/visualize/run-assembly-visualizer.sh"
agp2assamble = "/scratch/project_mnt/S0030/jingping/software/juicebox_scripts/juicebox_scripts/agp2assembly.py"
py2 = "/scratch/project_mnt/S0030/jingping/software/Python-2.7.10/bin/python2"
bwa = "/scratch/project/qaafi-cnafs/jingping/software/miniconda3/bin/bwa"
R2 = "/scratch/project_mnt/S0030/jingping/01.Banana_genome_project/15.ragtag2hic/fastq/HiC_R2.fastq.gz"
R1 = "/scratch/project_mnt/S0030/jingping/01.Banana_genome_project/15.ragtag2hic/fastq/HiC_R1.fastq.gz"
#ref = "/scratch/project_mnt/S0030/jingping/01.Banana_genome_project/15.ragtag2hic/genome.fa"
bam = "bwa_align.bam"
ref = sys.argv[1]
agp = sys.argv[2]
cmd = f"{bwa} index {ref}\n"
cmd += f"{bwa} mem -SP5M -t 80 {ref} {R1} {R2} | samtools view -hF 268 - | samtools sort -n -@ 80 -o {bam} -T tmp.align \n"
#cmd += f"{samtools} sort -n -@ 80 -o aligned.sort_name.bam {bam}\n"
cmd += f"{matlock} bam2 juicer {bam} merged_nodups.txt\n"
cmd += f"{py2} {agp2assamble} {agp} groups.assembly\n"
cmd += f"bash {assvis} -q 1 -p true groups.assembly merged_nodups.txt"
print(cmd)
cmd_sh = "head -n 15 /scratch/project_mnt/S0030/jingping/01.Banana_genome_project/15.ragtag2hic/ragtag2hic.slum > ragtag2hic.slum\n"
cmd_sh += f"echo \"{cmd}\" >> ragtag2hic.slum\n"
cmd_sh += "sbatch ragtag2hic.slum"
call(cmd_sh, shell=True)
# for i in cmd.split("\n"):
#     #print(i)
#     s = call(i, shell=True)
#     if s != 0:
#         print("error happened...cmd \"%s\" "%(i.strip()))
#         exit(0)
